//
//  SelectWatermarkPreviewView.swift
//  WatermarkCamera
//
//  Created by 赵新 on 6/7/2024.
//

import UIKit
import AxcBedrock

// MARK: - [SelectWatermarkPreviewView]

class SelectWatermarkPreviewView: BaseView {
    lazy var watermarkContentView: WatermarkContentView = {
        let view = WatermarkContentView()
        view.watermarkBigMarginsCalibration = [.right, .top, .left, .bottom]
        view.isUserInteractionEnabled = false
        view.isLockReverse = false
        view.isLockPortrait = true
        return view
    }()

    lazy var control: UIControl = {
        let control = UIControl()
        return control
    }()

    lazy var selectView: WatermarkSelectView = {
        let view = WatermarkSelectView()
        return view
    }()

    /// 点击回调
    var didSelectWatermarkBlock: AxcBlock.TwoParam<IndexPath, WatermarkDetailModel>?
    /// 点击无水印回调
    var didSelectEmptyWatermarkBlock: AxcBlock.Empty?
    /// 水印分享回调
    var shareWatermarkBlock: AxcBlock.OneParam<WatermarkDetailModel>?
    /// 编辑水印回调
    var editWatermarkBlock: AxcBlock.OneParam<WatermarkDetailModel>?
    /// 消失回调
    var dismissBlock: AxcBlock.Empty?
    // 水印数据加载完成
    var watermarkListLoadCompletion: AxcBlock.OneParam<[WatermarkDetailModel]>?

    override func createUI() {
        backgroundColor = .black.axc.alpha(0.2)

        addSubview(selectView)
        selectView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(440.cmera_autoVer + SafeAreaBottom)
        }

        addSubview(watermarkContentView)
        watermarkContentView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(ScreenHeight)
            make.bottom.equalTo(selectView.snp.top)
        }

        addSubview(control)
        control.snp.makeConstraints { make in
            make.edges.equalTo(watermarkContentView)
        }
    }

    override func bindForwardAction() {
        // 点水印部分消失
        control.axc.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.dismiss()
        }

        // 点X消失
        selectView.titleView.rightBtn.axc.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.dismiss()
        }
        
        // 选择空水印
        selectView.didSelectEmptyWatermarkBlock = { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.didSelectEmptyWatermarkBlock?()
            weakSelf.dismiss()
        }

        // 选择水印
        selectView.didSelectWatermarkBlock = { [weak self] indexPath, wmModel in
            guard let weakSelf = self else { return }
            weakSelf.didSelectWatermark(indexPath: indexPath, watermarkModel: wmModel)
            weakSelf.setWatermarkContentModel(wmModel)
        }
        // 加载水印回调
        selectView.watermarkListLoadCompletion = { [weak self] list in
            guard let weakSelf = self else { return }
            weakSelf.watermarkListLoadCompletion?(list)
        }
    }

    /// 选择水印后
    func didSelectWatermark(indexPath: IndexPath, watermarkModel: WatermarkDetailModel) {
        // 回调
        didSelectWatermarkBlock?(indexPath, watermarkModel)
    }

    /// 设置水印
    func setWatermarkContentModel(_ watermarkModel: WatermarkDetailModel?) {
        watermarkContentView.setWatermark(model: watermarkModel)
        watermarkContentView.layoutWatermarksBlock = { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.watermarkContentView.moveWatermarkView(CGPoint(x: 0, y: weakSelf.watermarkContentView.axc.height))
        }
    }
}

// MARK: AlertViewProtocol

extension SelectWatermarkPreviewView: AlertViewProtocol {
    /// 显示
    func show(selectWatermark: WatermarkDetailModel?) {
        selectView.viewModel.loadWatermarkList() // 加载水印
        // 加载水印
        selectView.setSelectWatermark(selectWatermark)
        setWatermarkContentModel(selectWatermark)
        let config: PresentToolVC.Config =
            .basicConfig(view: self, style: .sheet(.init(startHeight: ScreenHeight)), layoutBlock: { make in
                make.edges.equalToSuperview()
            })
        show(config: config)
    }
}
